package com.huika.dealontop.control.homepage.utils.cartproductnumber;

import android.content.Context;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.hk.app.android.lib.http.AjaxParams;
import com.hk.app.android.lib.http.VolleyRequestManager;
import com.huika.dealontop.R;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.ServerFlagError;
import com.huika.dealontop.utils.UIUtils;
import com.huika.dealontop.views.ToastMsg;

import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by cailu on 2016/1/13.
 */
public class BaseNet implements Response.ErrorListener {
    private Context mConxtext;
    BaseNet(Context context){
        mConxtext = context;
    }
    private Set<Object> tags = new HashSet<Object>();
    /**
     * 添加网络请求
     *
     * @param request
     */
    protected void executeRequest(FormResultRequest request) {
        AjaxParams ajaxParams = request.getRequestParams();
        VolleyRequestManager.addRequest(request, this);
        tags.add(this);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        String msg = UIUtils.getString(R.string.error_response);
        if (error instanceof ServerFlagError) {
            msg = ((ServerFlagError) error).result.msg;
            Log.e("ok",msg);
        } else {
            try {
                if (error.networkResponse != null && error.networkResponse.data != null)
                    msg = new String(error.networkResponse.data, "utf-8");
                Log.e("ok",msg);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                Log.e("ok", e.toString());
            }
        }
        showToastMsg(msg);
    }

    public void showToastMsg(String msg) {
        ToastMsg.showToastMsg(mConxtext, msg);
    }
}
